package com.health.manage.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.manage.mapper.UserMapper;
import com.health.manage.pojo.dto.UserDto;
import com.health.manage.pojo.entity.User;
import com.health.manage.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 *
 * @Description: 业务处理
 * @author: user
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

	@Autowired(required = false)
	private UserMapper userMapper;

	@Override
	public User login(String account, String password) {

		User user = null;
		try{
			QueryWrapper<User> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("account", account);
			queryWrapper.eq("password", password);
			user = this.userMapper.selectOne(queryWrapper);
		}catch (Exception e){
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public Boolean addData(User user) {
		int result = this.userMapper.insert(user);
		if(result > 1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public Boolean updateData(User user) {
		int result = this.userMapper.updateById(user);
		if(result > 1){
			return true;
		}else{
			return false;
		}
	}


	@Override
	public Boolean deleteData(Long id) {
		int result = this.userMapper.deleteById(id);
		if(result > 1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public Page<User> getPageData(UserDto userDto) {

		Page<User> page = new Page<>(userDto.getCurrentPage(), userDto.getPageSize());
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		if(userDto.getId()!=null && !"".equals(userDto.getId())){
			queryWrapper.eq("id", userDto.getId());
		}
		if(userDto.getName()!=null && !"".equals(userDto.getName())){
			queryWrapper.like("name", userDto.getName());
		}
		if(userDto.getAccount()!=null && !"".equals(userDto.getAccount())){
			queryWrapper.like("account", userDto.getAccount());
		}
		if(userDto.getPhone()!=null && !"".equals(userDto.getPhone())){
			queryWrapper.like("phone", userDto.getPhone());
		}
		if(userDto.getRole()!=null && !"".equals(userDto.getRole())){
			queryWrapper.like("role", userDto.getRole());
		}

		return this.userMapper.selectPage(page, queryWrapper);
	}


}
